package cn.calm.parameter;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;

import org.apache.log4j.Logger;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import cn.calm.commons.dao.IBaseDao;
import cn.calm.commons.provider.SessionProvider;
import cn.calm.entity.Parameter;
import cn.calm.parameter.provider.ParameterProvider;
import cn.calm.parameter.service.IParameterService;
import cn.calm.parameter.service.impl.ParameterServiceImpl;

public class Activator implements BundleActivator {
	public static BundleContext context;
	private static Logger log=Logger.getLogger(Activator.class);
	@Override
	public void start(BundleContext arg0) throws Exception {
		log.debug("starting");
		try {
			context=arg0;
			IParameterService<Parameter> parameterService=new ParameterServiceImpl<Parameter>();
			ServiceReference<IBaseDao> serviceRef= context.getServiceReference(IBaseDao.class);
			IBaseDao dao= context.getService(serviceRef);
			EntityManager em=Persistence.createEntityManagerFactory("entity").createEntityManager();
			dao.setSession(em);
			parameterService.setBaseDao(dao);
			
			context.registerService(IParameterService.class, parameterService, null);
			
			ParameterProvider provider=new ParameterProvider();
			provider.setParameterService(parameterService);
			context.registerService(SessionProvider.class, provider, null);
		} catch (Exception e) {
			log.error(e);
		}finally{
			log.debug("started");
		}
	}

	@Override
	public void stop(BundleContext arg0) throws Exception {
	}
}
